/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.CommoditySystem;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JViewport;
import model.Commodity;

/**
 *
 * @author Benjamin
 */
public class commoditiesGUI extends javax.swing.JFrame {

    private CommoditySystem system;
    
    private JPanel shoppingListPanel;
    //PRequest = ProductionRequest.
    private JPanel PRequestListPanel;

    /**
     * Creates new form commoditiesGUI
     */
    public commoditiesGUI() {
        //Liste med CommodityListItems.
        ArrayList<CommodityListItem> commodityList = new ArrayList<>();
        ArrayList<Commodity> commodities = new ArrayList<>();
        initComponents();
        
//        createShoppingListPanel();
//        createPRequestListPanel();

        //Sæt rammen i midten af skærmen.
        this.setLocationRelativeTo(null);

        try {
            system = CommoditySystem.getInstance();
        } catch (SQLException ex) {
        } catch (FileNotFoundException ex) {
        }

        //Opret forbindelse til databasen.
        try {
            system.connect();

            //Hent råvarer fra databasen:
            commodities = system.getCommodities();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } catch (FileNotFoundException ex) {
            System.out.println("Filen blev ikke fundet.");
        }

        //Vis tilgængelige råvarer på listen.
        for (Commodity commodity : commodities) {

            commodityList.add(new CommodityListItem(commodity));
        }

        createCommodityList(commodityList);
        
    }

    public JPanel getShoppingListPanel() {
        return shoppingListPanel;
    }

    public void setShoppingListPanel(JPanel shoppingListPanel) {
        this.shoppingListPanel = shoppingListPanel;
    }

    public JPanel getPRequestListPanel() {
        return PRequestListPanel;
    }

    public void setPRequestListPanel(JPanel PRequestListPanel) {
        this.PRequestListPanel = PRequestListPanel;
    }

    public static void addPanel(JPanel panel, int count, CommodityListItem listItem) {
        int x = 3;
        listItem.setLocation(x, (count * listItem.getHeight()) + x);
        listItem.setVisible(true);
        panel.add(listItem);
        panel.revalidate();
        panel.repaint();
    }

    public void removePanels(JPanel panel) {
        Component[] components = panel.getComponents();

        for (Component component : components) {
            jPanel2.remove(component);
        }

        panel.revalidate();
        panel.repaint();
    }

    public void createCommodityList(ArrayList<CommodityListItem> commodityList) {
        removePanels(jPanel2);

        int count = 0;

        Dimension dim = new Dimension(300, commodityList.size() * CommodityListItem.getHEIGHT());
        jPanel2.setPreferredSize(dim);

        for (CommodityListItem commodityList1 : commodityList) {
            addPanel(jPanel2, count, commodityList1);
            count++;
        }
    }
    
    public void createShoppingListPanel(){
        
        shoppingListPanel = new JPanel();
        
        //Definér størrelse.
        int x = jPanel7.getWidth();
        int y = jPanel7.getY();
        int width = jPanel5.getWidth() - jPanel7.getWidth();
        int height = jPanel5.getHeight();
        shoppingListPanel.setBounds(x, y, width, height);
        
        shoppingListPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        
        jPanel5.add(shoppingListPanel);
        
        shoppingListPanel.setVisible(false);
        
    }
    
    public void createPRequestListPanel(){
        PRequestListPanel = new JPanel();
        
        //Definér størrelse.
        int x = jPanel7.getWidth();
        int y = jPanel7.getY();
        int width = jPanel5.getWidth() - jPanel7.getWidth();
        int height = jPanel5.getHeight();
        PRequestListPanel.setBounds(x, y, width, height);
        
        PRequestListPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        
        jPanel5.add(PRequestListPanel);
        
        PRequestListPanel.setVisible(false);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel10 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(1200, 720));
        getContentPane().setLayout(null);

        jPanel1.setPreferredSize(new java.awt.Dimension(1280, 720));
        jPanel1.setLayout(null);

        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel3.setLayout(null);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jPanel2.setLayout(null);
        jScrollPane1.setViewportView(jPanel2);

        jPanel3.add(jScrollPane1);
        jScrollPane1.setBounds(10, 50, 310, 530);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("Råvarelager");
        jPanel3.add(jLabel3);
        jLabel3.setBounds(10, 10, 220, 30);

        jPanel1.add(jPanel3);
        jPanel3.setBounds(860, 90, 330, 590);

        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setLayout(null);

        jPanel6.setBackground(new java.awt.Color(204, 204, 204));
        jPanel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jPanel6.setLayout(null);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/ink_logo.png"))); // NOI18N
        jLabel1.setFocusable(false);
        jPanel6.add(jLabel1);
        jLabel1.setBounds(0, 0, 100, 70);

        jPanel4.add(jPanel6);
        jPanel6.setBounds(10, 10, 100, 70);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel2.setText("RÅVARER");
        jPanel4.add(jLabel2);
        jLabel2.setBounds(120, 10, 410, 70);

        jPanel1.add(jPanel4);
        jPanel4.setBounds(0, 0, 1190, 90);

        jPanel5.setBackground(new java.awt.Color(200, 200, 200));
        jPanel5.setLayout(null);

        jPanel7.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel7.setLayout(null);

        jPanel8.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel8.setLayout(null);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel4.setText("Produktionsordrer");
        jPanel8.add(jLabel4);
        jLabel4.setBounds(10, 10, 220, 30);

        jPanel10.setLayout(null);

        jPanel12.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel12.setLayout(null);

        jLabel6.setText("17-02-2015 12:00:00");
        jPanel12.add(jLabel6);
        jLabel6.setBounds(10, 30, 120, 14);

        jLabel7.setText("18-02-2015 12:00:00");
        jPanel12.add(jLabel7);
        jLabel7.setBounds(10, 70, 120, 14);

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane4.setViewportView(jList2);

        jPanel12.add(jScrollPane4);
        jScrollPane4.setBounds(160, 10, 80, 80);

        jButton2.setText("Tilføj");
        jPanel12.add(jButton2);
        jButton2.setBounds(260, 60, 60, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("ETA");
        jPanel12.add(jLabel8);
        jLabel8.setBounds(10, 50, 110, 14);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("Productiondate");
        jPanel12.add(jLabel9);
        jLabel9.setBounds(10, 10, 110, 14);

        jPanel10.add(jPanel12);
        jPanel12.setBounds(0, 0, 330, 100);

        jScrollPane2.setViewportView(jPanel10);

        jPanel8.add(jScrollPane2);
        jScrollPane2.setBounds(10, 50, 330, 530);

        jPanel7.add(jPanel8);
        jPanel8.setBounds(0, 0, 350, 590);

        jPanel9.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel9.setLayout(null);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel5.setText("Indkøbsliste");
        jPanel9.add(jLabel5);
        jLabel5.setBounds(10, 10, 220, 30);

        jPanel11.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));
        jPanel11.setLayout(null);

        jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "17-02-2015 12:00:00", "24-02-2015 12:00:00" }));
        jPanel11.add(jComboBox1);
        jComboBox1.setBounds(10, 10, 470, 30);

        jList1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Tshirt: \t\tamount: 50", "Mug:  \t\tamount: 80", "Paper: \t\tamount: 30", "Businesscard: \tamount: 05", "Laminat: \t\tamount: 60", "Binder:\t\tamount: 50", "Ink:\t\tamount: 30" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(jList1);

        jPanel11.add(jScrollPane3);
        jScrollPane3.setBounds(10, 50, 470, 410);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jButton1.setText("Opret indkøbsliste");
        jPanel11.add(jButton1);
        jButton1.setBounds(10, 470, 470, 50);

        jPanel9.add(jPanel11);
        jPanel11.setBounds(10, 50, 490, 530);

        jPanel7.add(jPanel9);
        jPanel9.setBounds(350, 0, 510, 590);

        jPanel5.add(jPanel7);
        jPanel7.setBounds(0, 0, 860, 590);

        jPanel1.add(jPanel5);
        jPanel5.setBounds(0, 90, 860, 590);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 1190, 690);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(commoditiesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(commoditiesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(commoditiesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(commoditiesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new commoditiesGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    // End of variables declaration//GEN-END:variables
}
